{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入相关库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:39:19.353626Z",
     "start_time": "2018-05-11T04:39:16.740770Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# from matplotlib import pyplot as plt\n",
    "#%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:39:22.337800Z",
     "start_time": "2018-05-11T04:39:19.360040Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "from sklearn import svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:39:38.803049Z",
     "start_time": "2018-05-11T04:39:22.343146Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "from keras.applications.vgg16 import VGG16\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.vgg16 import preprocess_input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:32:24.951424Z",
     "start_time": "2018-05-07T11:32:24.949126Z"
    }
   },
   "outputs": [],
   "source": [
    "from keras.applications.mobilenet import MobileNet\n",
    "from keras.applications.mobilenet import preprocess_input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取VGG16预训练模型参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:39:53.430402Z",
     "start_time": "2018-05-11T04:39:38.809119Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "model_vgg16 = VGG16(weights='imagenet', include_top=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-04-03T07:15:28.562844Z",
     "start_time": "2018-04-03T07:15:24.117326Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true,
     "marked": true
    }
   },
   "outputs": [],
   "source": [
    "model_vgg16 = MobileNet(input_shape=(224, 224, 3), weights='imagenet', include_top=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取训练集图片计算特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:40:49.267852Z",
     "start_time": "2018-05-11T04:39:53.436061Z"
    },
    "code_folding": [],
    "deletable": false,
    "editable": false,
    "run_control": {
     "marked": false
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_features = []\n",
    "for i in list(range(1, 401)) + list(range(501, 901)):\n",
    "    img_path = 'test8/%06d.png' % i\n",
    "    # print(img_path)\n",
    "    img = image.load_img(img_path, target_size=(224, 224))# 读取图片\n",
    "    \n",
    "    #plt.imshow(img)\n",
    "    #plt.show()# 显示图片\n",
    "    x = image.img_to_array(img)# 图片转换为ndarray\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)# 数组转换为vgg16输入格式\n",
    "\n",
    "    training_set_vgg16_features.append(model_vgg16.predict(x).reshape((7*7*512, )))# 计算该张图片的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:40:49.443221Z",
     "start_time": "2018-05-11T04:40:49.411151Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_features_ndarray = np.vstack(training_set_vgg16_features)# 转换为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:40:49.573130Z",
     "start_time": "2018-05-11T04:40:49.570921Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "training_set_label = np.array([1.0 if i < 400 else 0.0 for i in range(800)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取测试集图片计算特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:01.317904Z",
     "start_time": "2018-05-11T04:40:49.711922Z"
    },
    "code_folding": [],
    "deletable": false,
    "editable": false,
    "run_control": {
     "marked": false
    }
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_features = []\n",
    "for i in list(range(401, 501)) + list(range(901, 1001)):\n",
    "    img_path = 'test8/%06d.png' % i\n",
    "    # print(img_path)\n",
    "    img = image.load_img(img_path, target_size=(224, 224))# 读取图片\n",
    "    #plt.imshow(img)\n",
    "    #plt.show()# 显示图片\n",
    "    x = image.img_to_array(img)# 图片转换为ndarray\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)# 数组转换为vgg16输入格式\n",
    "\n",
    "    test_set_vgg16_features.append(model_vgg16.predict(x).reshape((7*7*512, )))# 计算该张图片的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:01.494326Z",
     "start_time": "2018-05-11T04:41:01.484367Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_features_ndarray = np.vstack(test_set_vgg16_features)# 转换为ndarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# VGG16+SVM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义SVM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:27.822328Z",
     "start_time": "2018-05-07T11:51:27.819929Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "clf_vgg = svm.SVC()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 训练SVM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:41.069353Z",
     "start_time": "2018-05-07T11:51:28.419641Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "clf_vgg.fit(training_set_vgg16_features_ndarray, training_set_label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算SVM在训练集上的预测值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:57.381422Z",
     "start_time": "2018-05-07T11:51:41.590130Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_with_svm_prediction = clf_vgg.predict(training_set_vgg16_features_ndarray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:58.094374Z",
     "start_time": "2018-05-07T11:51:58.090533Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_with_svm_prediction[training_set_vgg16_with_svm_prediction >= 0.5] = 1\n",
    "training_set_vgg16_with_svm_prediction[training_set_vgg16_with_svm_prediction < 0.5] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:58.753184Z",
     "start_time": "2018-05-07T11:51:58.747083Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_with_svm_prediction_positive = training_set_vgg16_with_svm_prediction[: 400]\n",
    "training_set_vgg16_with_svm_prediction_negative = training_set_vgg16_with_svm_prediction[400:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算训练集准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:51:59.585711Z",
     "start_time": "2018-05-07T11:51:59.575004Z"
    }
   },
   "outputs": [],
   "source": [
    "print('VGG16+SVM Training Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(training_set_vgg16_with_svm_prediction_positive[training_set_vgg16_with_svm_prediction_positive == 1]),len(training_set_vgg16_with_svm_prediction_positive[training_set_vgg16_with_svm_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(training_set_vgg16_with_svm_prediction_negative[training_set_vgg16_with_svm_prediction_negative == 0]),len(training_set_vgg16_with_svm_prediction_negative[training_set_vgg16_with_svm_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(training_set_vgg16_with_svm_prediction_positive[training_set_vgg16_with_svm_prediction_positive == 1]) + 0.0) / len(training_set_vgg16_with_svm_prediction_positive),\n",
    "    (len(training_set_vgg16_with_svm_prediction_negative[training_set_vgg16_with_svm_prediction_negative == 1]) + 0.0) / len(training_set_vgg16_with_svm_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算SVM在测试集上的预测值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:52:03.825119Z",
     "start_time": "2018-05-07T11:52:00.382998Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_with_svm_prediction = clf_vgg.predict(test_set_vgg16_features_ndarray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:52:04.549212Z",
     "start_time": "2018-05-07T11:52:04.543435Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_with_svm_prediction[test_set_vgg16_with_svm_prediction >= 0.5] = 1\n",
    "test_set_vgg16_with_svm_prediction[test_set_vgg16_with_svm_prediction < 0.5] = 0\n",
    "test_set_vgg16_with_svm_prediction_positive = test_set_vgg16_with_svm_prediction[: 100]\n",
    "test_set_vgg16_with_svm_prediction_negative = test_set_vgg16_with_svm_prediction[100:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算测试集准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T11:52:05.367111Z",
     "start_time": "2018-05-07T11:52:05.354019Z"
    }
   },
   "outputs": [],
   "source": [
    "print('VGG16+SVM test Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(test_set_vgg16_with_svm_prediction_positive[test_set_vgg16_with_svm_prediction_positive == 1]),len(test_set_vgg16_with_svm_prediction_positive[test_set_vgg16_with_svm_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(test_set_vgg16_with_svm_prediction_negative[test_set_vgg16_with_svm_prediction_negative == 0]),len(test_set_vgg16_with_svm_prediction_negative[test_set_vgg16_with_svm_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(test_set_vgg16_with_svm_prediction_positive[test_set_vgg16_with_svm_prediction_positive == 1]) + 0.0) / len(test_set_vgg16_with_svm_prediction_positive),\n",
    "    (len(test_set_vgg16_with_svm_prediction_negative[test_set_vgg16_with_svm_prediction_negative == 1]) + 0.0) / len(test_set_vgg16_with_svm_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-01T08:39:33.807221Z",
     "start_time": "2018-02-01T08:39:33.803824Z"
    }
   },
   "source": [
    "# 全连接神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 导入相关库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:01.661200Z",
     "start_time": "2018-05-11T04:41:01.658941Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Activation, Dropout, Dense"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义全连接神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:01.866842Z",
     "start_time": "2018-05-11T04:41:01.823201Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "model_smoke_detector = Sequential()\n",
    "model_smoke_detector.add(Dense(1024, activation='sigmoid', input_shape=(7*7*512, )))# 加入全连接层\n",
    "model_smoke_detector.add(Dropout(0.5))# 加入dropout防止过拟合\n",
    "model_smoke_detector.add(Dense(128, activation='sigmoid'))# 加入全连接层\n",
    "model_smoke_detector.add(Dropout(0.5))# 加入dropout防止过拟合\n",
    "model_smoke_detector.add(Dense(1, activation='sigmoid'))# 加入全连接层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:02.061880Z",
     "start_time": "2018-05-11T04:41:02.033012Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.compile(optimizer='adam',\n",
    "                             loss='binary_crossentropy',\n",
    "                             metrics=['accuracy'])\n",
    "# 定义神经网络损失函数等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:37.772187Z",
     "start_time": "2018-05-11T04:41:02.237804Z"
    },
    "deletable": false,
    "editable": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.fit(training_set_vgg16_features_ndarray, training_set_label, nb_epoch=10, batch_size=16)\n",
    "# 拟合数据即训练神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:37.993665Z",
     "start_time": "2018-05-11T04:41:37.944270Z"
    },
    "deletable": false,
    "editable": false
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_with_fc_prediction = model_smoke_detector.predict(test_set_vgg16_features_ndarray)# 对测试集进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:38.278361Z",
     "start_time": "2018-05-11T04:41:38.172904Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_with_fc_prediction = model_smoke_detector.predict(training_set_vgg16_features_ndarray)# 对测试集进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:38.469693Z",
     "start_time": "2018-05-11T04:41:38.466429Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_vgg16_with_fc_prediction[test_set_vgg16_with_fc_prediction >= 0.5] = 1\n",
    "test_set_vgg16_with_fc_prediction[test_set_vgg16_with_fc_prediction < 0.5] = 0\n",
    "test_set_vgg16_with_fc_prediction_positive = test_set_vgg16_with_fc_prediction[:100]\n",
    "test_set_vgg16_with_fc_prediction_negative = test_set_vgg16_with_fc_prediction[100:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:38.687736Z",
     "start_time": "2018-05-11T04:41:38.684256Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_vgg16_with_fc_prediction[training_set_vgg16_with_fc_prediction >= 0.5] = 1\n",
    "training_set_vgg16_with_fc_prediction[training_set_vgg16_with_fc_prediction < 0.5] = 0\n",
    "training_set_vgg16_with_fc_prediction_positive = training_set_vgg16_with_fc_prediction[:400]\n",
    "training_set_vgg16_with_fc_prediction_negative = training_set_vgg16_with_fc_prediction[400:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:38.891122Z",
     "start_time": "2018-05-11T04:41:38.886016Z"
    }
   },
   "outputs": [],
   "source": [
    "print('VGG16+FC Training Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(training_set_vgg16_with_fc_prediction_positive[training_set_vgg16_with_fc_prediction_positive == 1]),len(training_set_vgg16_with_fc_prediction_positive[training_set_vgg16_with_fc_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(training_set_vgg16_with_fc_prediction_negative[training_set_vgg16_with_fc_prediction_negative == 0]),len(training_set_vgg16_with_fc_prediction_negative[training_set_vgg16_with_fc_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(training_set_vgg16_with_fc_prediction_positive[training_set_vgg16_with_fc_prediction_positive == 1]) + 0.0) / len(training_set_vgg16_with_fc_prediction_positive),\n",
    "    (len(training_set_vgg16_with_fc_prediction_negative[training_set_vgg16_with_fc_prediction_negative == 1]) + 0.0) / len(training_set_vgg16_with_fc_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:39.099096Z",
     "start_time": "2018-05-11T04:41:39.094211Z"
    }
   },
   "outputs": [],
   "source": [
    "print('VGG16+FC test Set Confusion Matrix\\n          Ture      False')\n",
    "\n",
    "print('Positive： {0}        {1}'.format(\n",
    "    len(test_set_vgg16_with_fc_prediction_positive[test_set_vgg16_with_fc_prediction_positive == 1]),len(test_set_vgg16_with_fc_prediction_positive[test_set_vgg16_with_fc_prediction_positive == 0])))\n",
    "print('Negative： {0}        {1}'.format(\n",
    "    len(test_set_vgg16_with_fc_prediction_negative[test_set_vgg16_with_fc_prediction_negative == 0]),len(test_set_vgg16_with_fc_prediction_negative[test_set_vgg16_with_fc_prediction_negative == 1])))\n",
    "print('检测率: {0}, 虚警率: {1}'.format(\n",
    "    (len(test_set_vgg16_with_fc_prediction_positive[test_set_vgg16_with_fc_prediction_positive == 1]) + 0.0) / len(test_set_vgg16_with_fc_prediction_positive),\n",
    "    (len(test_set_vgg16_with_fc_prediction_negative[test_set_vgg16_with_fc_prediction_negative == 1]) + 0.0) / len(test_set_vgg16_with_fc_prediction_negative)\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:41:39.587993Z",
     "start_time": "2018-05-11T04:41:39.306384Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.save('model_smoke_detector_vgg16.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T06:18:11.910812Z",
     "start_time": "2018-05-11T06:17:56.953588Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from keras.applications.vgg16 import VGG16\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.vgg16 import preprocess_input\n",
    "import cv2\n",
    "from keras.models import load_model\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T06:18:26.132518Z",
     "start_time": "2018-05-11T06:18:11.922854Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector = load_model('smoke_detecctor_with_vgg16.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T06:18:27.070230Z",
     "start_time": "2018-05-11T06:18:26.140903Z"
    }
   },
   "outputs": [],
   "source": [
    "model_vgg16 = VGG16(input_shape=(224, 224, 3), include_top=False, weights='imagenet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T06:25:38.340342Z",
     "start_time": "2018-05-11T06:24:54.355066Z"
    }
   },
   "outputs": [],
   "source": [
    "cap = cv2.VideoCapture('smoke_video/train-13.avi')\n",
    "video_writer = cv2.VideoWriter('smoke_detection_output2.avi', cv2.VideoWriter_fourcc(*'MJPG'), 24, (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))\n",
    "\n",
    "steps = [5, 150, 500]\n",
    "#frames = [0] * 30\n",
    "choice = 0\n",
    "total_time = 0\n",
    "count = 0\n",
    "#num = 1\n",
    "while True:\n",
    "    status, img = cap.read()\n",
    "    #img = img[:, ::-1, :]\n",
    "    if status:\n",
    "        #img = img[100: 500, 200: 600, :]\n",
    "        '''img_detect = cv2.resize(img[:, :, [2, 1, 0]], (48 * num, 48 * num))\n",
    "        detection_results = []\n",
    "        for i in range(num):\n",
    "            for j in range(num):\n",
    "                tmp = img_detect[i * 48: (i + 1) * 48, j * 48: (j + 1) * 48, :]\n",
    "                tmp = cv2.resize(tmp, (224, 224))\n",
    "                start_time = time.time()\n",
    "                x = image.img_to_array(tmp)\n",
    "                x = np.expand_dims(x, axis=0)\n",
    "                x = preprocess_input(x)\n",
    "                feature = model_vgg16.predict(x).reshape((1, 7*7*1024))\n",
    "                #print(model_smoke_detector.predict(feature)[0, 0])\n",
    "                result = model_smoke_detector.predict(feature)[0, 0]\n",
    "                detection_results.append(result)\n",
    "                end_time = time.time()\n",
    "\n",
    "                total_time += end_time - start_time\n",
    "                count += 1'''\n",
    "        \n",
    "        img_detect = cv2.resize(img[:, :, [2, 1, 0]], (224, 224))\n",
    "        #tmp = img_detect[i * 48: (i + 1) * 48, j * 48: (j + 1) * 48, :]\n",
    "        #tmp = cv2.resize(tmp, (224, 224))\n",
    "        start_time = time.time()\n",
    "        x = image.img_to_array(img_detect)\n",
    "        x = np.expand_dims(x, axis=0)\n",
    "        x = preprocess_input(x)\n",
    "        feature = model_vgg16.predict(x).reshape((1, 7*7*512))\n",
    "        #print(model_smoke_detector.predict(feature)[0, 0])\n",
    "        result = model_smoke_detector.predict(feature)[0, 0]\n",
    "        #detection_results.append(result)\n",
    "        end_time = time.time()\n",
    "\n",
    "        total_time += end_time - start_time\n",
    "        count += 1\n",
    "                \n",
    "        #frames.pop(0)\n",
    "        #frames.append(1 if result > 0.5 else 0)\n",
    "        img = cv2.flip(img, 1)\n",
    "        cv2.putText(img,\n",
    "                    'Probability: {0}'.format(str(result)),\n",
    "                    (30, 30), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    1, \n",
    "                    (0, 0, 255), \n",
    "                    1)\n",
    "        cv2.putText(img,\n",
    "                    '{0}'.format('smoke' if result > 0.5 else 'no smoke'),\n",
    "                    (30, 90), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    2, \n",
    "                    (0, 0, 255), \n",
    "                    2)\n",
    "        '''cv2.putText(img,\n",
    "                    '{0}'.format('smoke' if sum(frames) > 0 else 'no smoke'),\n",
    "                    (30, 90), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    2, \n",
    "                    (0, 0, 255), \n",
    "                    2)'''\n",
    "        cv2.imshow('', img)\n",
    "        key = cv2.waitKey(steps[choice])\n",
    "        video_writer.write(img)\n",
    "        if key == 27:\n",
    "            break\n",
    "        elif key == ord('n'):\n",
    "            choice += 1\n",
    "            choice %= 3\n",
    "    else:\n",
    "        cv2.destroyAllWindows()\n",
    "        break\n",
    "cap.release()\n",
    "video_writer.release()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T06:24:37.685726Z",
     "start_time": "2018-05-11T06:24:37.676628Z"
    }
   },
   "source": [
    "total_time / count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
